[class]Menusysteem met <ul> generatie
Auteur: Rubdos - 11 december 2008 - 12:02 - Gekeurd door: Gerard - Hits: 3426 - Aantal punten: 3.00 (2 stemmen)
Deze class genereert een menu met oneindig veel subitems. Voorbeeld:
include("class.menu.php");
$menu = new menu;
$id = $menu->addItem($naam,$url,$parent=-1);
$menu->addItem($naam2,$url2,$id);
$menu->writeHtml;
include("class.menu.php"); $menu = new menu; $id = $menu->addItem($naam,$url,$parent=-1); $menu->addItem($naam2,$url2,$id); $menu->writeHtml;
Nu bevat $id 0, omdat dat de eerste id is die wordt toegekend. het 2e item wordt dus onder het eerste gezet (subitem):
<ul>
<li>$naam
<ul>
<li>$naam2</li>
</ul>
</li>
</ul>
functies:
integer addItem($name,[$url [,$parent]]);
boolean hasSubItems($id);
string getHtmlOfSubItems($subitems);
writeHtml
Versie 1.1:
Wanneer geklikt op een item, wordt aan die link de class "selected" toegewezen. Url is niet meer verplicht.
|
Code: |
De Class:
<?php
class menu {
var $menuitems;
function addItem($name,$url="?",$parent = -1){
$this->menuitems[] = array("parent" => $parent,"url" => $url,"name" => $name);
return count($this->menuitems)-1;
}
function hasSubItems($item){
$result=false;
$menuitems=$this->menuitems;
foreach($menuitems as $key => $value){
if($value['parent']==$item){
$result=true;
}
}
return $result;
}
function getHtmlOfSubItems($subitem){
$key=0;
$menuitems=$this->menuitems;
foreach($menuitems as $key => $value){
if($value['parent'] == $subitem){
if($_GET['ItemId'] == $key){
$class = "class=\"selected\"";
}else{$class="";}
$returnhtml.= "<li><a $class href=\"".$value['url']."&ItemId=".$key."\">".$value['name']."</a>";
if($this->hasSubItems($key)){
$returnhtml.="<ul>\r\n";
$result = $this->getHtmlOfSubItems($key);
$returnhtml .= $result;
$returnhtml.="</ul>\r\n";
}
$returnhtml.= "</li>";
}
}
return $returnhtml;
}
function writeHtml(){
$html= "<ul>";
$html.= $this->getHtmlOfSubItems(-1);
$html.= "</ul>";
echo $html;
}
}
/* ---voorbeeldje---
$menu = new menu;
$menu->addItem("Home","?");
$menu->addItem("Links","?option=twee");
$menu->writeHtml();
*/
?>
<?php class menu { var $menuitems; function addItem($name,$url="?",$parent = -1){ $this->menuitems[] = array("parent" => $parent,"url" => $url,"name" => $name); return count($this->menuitems)-1; } function hasSubItems($item){ $result=false; $menuitems=$this->menuitems; foreach($menuitems as $key => $value){ if($value['parent']==$item){ $result=true; } } return $result; } function getHtmlOfSubItems($subitem){ $key=0; $menuitems=$this->menuitems; foreach($menuitems as $key => $value){ if($value['parent'] == $subitem){ if($_GET['ItemId'] == $key){ $class = "class=\"selected\""; }else{$class="";} $returnhtml.= "<li><a $class href=\"".$value['url']."&ItemId=".$key."\">".$value['name']."</a>"; if($this->hasSubItems($key)){ $returnhtml.="<ul>\r\n"; $result = $this->getHtmlOfSubItems($key); $returnhtml .= $result; $returnhtml.="</ul>\r\n"; } $returnhtml.= "</li>"; } } return $returnhtml; } function writeHtml(){ $html= "<ul>"; $html.= $this->getHtmlOfSubItems(-1); $html.= "</ul>"; } } /* ---voorbeeldje--- $menu = new menu; $menu->addItem("Home","?"); $menu->addItem("Links","?option=twee"); $menu->writeHtml(); */ ?>
Download code (.txt)
|
|
Stemmen |
Niet ingelogd. |
|